package com.haojiang.mh.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.haojiang.mh.R;
import com.haojiang.mh.utils.SizeUtil;

/**
 * 得分评价View
 * <p>
 * Created by ZapFive on 2019-05-25.
 * <p>
 * wuzhuang@mirahome.me
 */
public class EvaluateView extends View {

    private Paint rectPaint;//边框
    private TextPaint textPaint;//文本
    private int score;//得分
    private String statusText = "";//评价
    private int padding;//间隔
    private int commonColor = 0xFF79D9C8;//通用颜色
    private int defaultWidth, defaultHeight; //默认宽高
    private RectF rectF = new RectF();//矩形
    private int colorGreen, colorOrange, colorWhite;

    public EvaluateView(Context context) {
        this(context, null);
    }

    public EvaluateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EvaluateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        padding = SizeUtil.dp2px(context, 1);

        defaultWidth = SizeUtil.dp2px(context, 45) + padding * 2;
        defaultHeight = SizeUtil.dp2px(context, 17) + padding * 2;

        colorGreen = getResources().getColor(R.color.green_light);
        colorOrange = getResources().getColor(R.color.origin_light);
        colorWhite = getResources().getColor(R.color.white);

        rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(SizeUtil.dp2px(context, 1));

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(SizeUtil.sp2px(context, 12));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (TextUtils.isEmpty(statusText)) return;
        rectPaint.setColor(commonColor);
        rectF.set(padding, padding, getWidth() - padding, getHeight() - padding);
        int cxy = getHeight() / 2;
        canvas.drawRoundRect(rectF, cxy, cxy, rectPaint);

        textPaint.setColor(commonColor);
        canvas.drawText(
                statusText,
                getWidth() * 0.5f,
                SizeUtil.getBaseLine(textPaint, getHeight(), getHeight()),
                textPaint
        );
    }

    public void setShowText(String text) {
        switch (text) {
            case "normal":
                statusText = getResources().getString(R.string.tv_normal);
                commonColor = colorGreen;
                break;
            case "low":
                commonColor = colorOrange;
                statusText = getResources().getString(R.string.tv_low);
                break;
            case "high":
                commonColor = colorOrange;
                statusText = getResources().getString(R.string.tv_high);
                break;
            case "much":
                commonColor = colorOrange;
                statusText = getResources().getString(R.string.tv_much);
                break;
            case "good":
                commonColor = colorWhite;
                statusText = getResources().getString(R.string.tv_good);
                break;
            case "not_good":
                commonColor = colorOrange;
                statusText = getResources().getString(R.string.tv_not_good);
                break;
            case "great":
                commonColor = colorGreen;
                statusText = getResources().getString(R.string.tv_great);
                break;
            case "":
                statusText = "";
                break;
        }
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(
                getMeasuredLength(widthMeasureSpec, true),
                getMeasuredLength(heightMeasureSpec, false)
        );
    }

    private int getMeasuredLength(int length, boolean isWidth) {
        int specMode = MeasureSpec.getMode(length);
        int specSize = MeasureSpec.getSize(length);
        int size;
        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
        if (specMode == MeasureSpec.EXACTLY) {
            size = specSize;
        } else if (specMode == MeasureSpec.UNSPECIFIED) {
            size = isWidth ? defaultWidth : defaultHeight;
        } else {
            size = isWidth ? padding + defaultWidth : padding + defaultHeight;
            if (specMode == MeasureSpec.AT_MOST) {
                size = Math.min(size, specSize);
            }
        }
        return size;
    }
}
